/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.blog.city.checkthedep.managedbeans;

import com.blog.city.checkthedep.entities.Article;
import com.blog.city.checkthedep.entities.Comment;
import com.blog.city.checkthedep.sessionbeans.ArticleFacadeLocal;
import com.blog.city.checkthedep.sessionbeans.CommentFacadeLocal;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.ejb.EJB;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import org.primefaces.context.RequestContext;

/**
 *
 * @author Breath
 */
@ManagedBean
@SessionScoped
public class CommentBean {

    private Comment comment = new Comment();
    private String commentBody = "";
    private Article articleForComment = new Article();
    private List<Comment> articleComments;
    private List<Comment> latestComment;
    @EJB
    private CommentFacadeLocal commFac;
    @EJB
    private ArticleFacadeLocal artFac;
    private Integer artI;

    /**
     * 
     */
    @PostConstruct
    public void retrieveLatest() {
        this.setLatestComment(commFac.latestComments());
    }

    public void retrieveComments() {
        this.setArticleComments(commFac.findByArticle(articleForComment));
    }

    public Article getArticleForComment() {
        return articleForComment;
    }

    public void setArticleForComment(Article articleForComment) {
        this.articleForComment = articleForComment;
    }

    public List<Comment> getArticleComments() {
        return articleComments;
    }

    public void setArticleComments(List<Comment> articleComments) {
        this.articleComments = articleComments;
    }

    /**
     * 
     * @return
     */
    public Integer getArtI() {
        return artI;
    }

    /**
     * 
     * @param artI
     */
    public void setArtI(Integer artI) {
        this.artI = artI;
    }

    /**
     * 
     * @return
     */
    public String getCommentBody() {
        return commentBody;
    }

    /**
     * 
     * @param commentBody
     */
    public void setCommentBody(String commentBody) {
        this.commentBody = commentBody;
    }
    @ManagedProperty(value = "#{loginBean}")
    private LoginBean loginBean;

    /**
     * 
     * @return
     */
    public LoginBean getLoginBean() {
        return loginBean;
    }

    /**
     * 
     * @param loginBean
     */
    public void setLoginBean(LoginBean loginBean) {
        this.loginBean = loginBean;
    }

    /**
     * 
     */
    public void setAuthor() {
        comment.setRegisteredUserId(loginBean.getCheckedUser());
    }

    /**
     * 
     */
    public void setArticle() {
        comment.setArticleId(artFac.find(this.artI));
    }

    /**
     * 
     * @return
     */
    public CommentFacadeLocal getCommFac() {
        return commFac;
    }

    /**
     * 
     * @param commFac
     */
    public void setCommFac(CommentFacadeLocal commFac) {
        this.commFac = commFac;
    }

    /**
     * 
     * @return
     */
    public Comment getComment() {
        return comment;
    }

    /**
     * 
     * @param comment
     */
    public void setComment(Comment comment) {
        this.comment = comment;
    }

    /**
     * 
     */
    public void persistComment() {
        if (commFac.exists(comment)) {
            comment = new Comment();
            comment.setCommentBody(commentBody);
            setAuthor();
            setArticle();
            commFac.create(comment);
        } else {
            if (comment != null) {
                comment.setCommentBody(commentBody);
                setAuthor();
                setArticle();
                commFac.create(comment);
            }
        }
    }
    
    
    /**
     * 
     */
    public void persistForVisitor() {
        if (commFac.exists(comment)) {
            comment = new Comment();
            comment.setCommentBody(commentBody);
            setArticle();
            commFac.create(comment);
        } else {
            if (comment != null) {
                comment.setCommentBody(commentBody);
                setArticle();
                commFac.create(comment);
            }
        }
    }

    /**
     * 
     */
    public void persistEditedComment() {
        if (comment != null) {
            commFac.edit(comment);
        }
    }

    /**
     * 
     * @param id
     */
    public void deleteSComment(Integer id) {
        if (comment != null) {
            commFac.remove(comment);
        }
    }

    /**
     * 
     * @return
     */
    public List<Comment> getLatestComment() {
        return latestComment;
    }

    /**
     * 
     * @param latestComment
     */
    public void setLatestComment(List<Comment> latestComment) {
        this.latestComment = latestComment;
    }
}
